<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.keywords" placeholder="楼层名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="dataForm.time"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.type" placeholder="类型" value="">
          <el-option :key="0" label="商品分类" :value="0"></el-option>
          <el-option :key="1" label="联盟店分类" :value="1"></el-option>
          <el-option :key="2" label="活动" :value="2"></el-option>
          <el-option :key="3" label="外链" :value="3"></el-option>
          <el-option :key="4" label="店铺" :value="4"></el-option>
          <!-- <el-option :key="5" label="专题" :value="5"></el-option> -->
          <el-option :key="6" label="无跳转" :value="6"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="pageIndex = 1
        getDataList()">查询</el-button>
        <el-button v-if="isAuth('home:floor:add')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('home:floor:remove')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="floorName"
        header-align="center"
        align="center"
        label="楼层名称">
      </el-table-column>
      <el-table-column
        prop=""
        header-align="center"
        align="center"
        label="图片">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            trigger="hover">
            <img :src="`http://image.yilianbaihui.cn/consumer/${scope.row.imgPath}/${scope.row.imgName}`" style="max-height: 300px;"/>
            <img slot="reference" :src="`http://image.yilianbaihui.cn/consumer/${scope.row.imgPath}/${scope.row.imgName}`" style="max-height: 50px;max-width: 130px">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        prop="jumpType"
        header-align="center"
        align="center"
        label="类型">
        <template slot-scope="scope">
          <span v-if="scope.row.jumpType === 0">商品分类</span>
          <span v-if="scope.row.jumpType === 1">联盟店分类</span>
          <span v-if="scope.row.jumpType === 2">活动</span>
          <span v-if="scope.row.jumpType === 3">外链</span>
          <span v-if="scope.row.jumpType === 4">店铺</span>
          <span v-if="scope.row.jumpType === 5">专题</span>
          <span v-if="scope.row.jumpType === 6">无跳转</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="seq"
        header-align="center"
        align="center"
        label="排序">
      </el-table-column>
      <el-table-column
        prop="showStatus"
        header-align="center"
        align="center"
        label="显示">
        <template slot-scope="scope">
          <!--{{scope.row.showStatus?'显示':'不显示'}}-->
          <el-switch
            @change="showStatusChange(scope.row.id,scope.row.showStatus)"
            v-model="scope.row.showStatus"
            active-text="显示"
            inactive-text="不显示">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" v-if="isAuth('home:floor:modify')"  @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" v-if="isAuth('home:floor:remove')"  @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './home-floor-add-or-update.vue'
  export default {
    data () {
      return {
        dataForm: {
          keywords: '',
          time: [],
          type: ''
        },
        parentClassId: 0,
        updateData: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        dataListSelections: []
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/home/floor/queryFloorList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keywords': this.dataForm.keywords,
            'startTime': this.dataForm.time[0],
            'endTime': this.dataForm.time[1],
            'bannerType': this.dataForm.type
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.dataList.map(list => {
              list.showStatus = Boolean(list.showStatus)
            })
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      deleteHandle (id) {
        let ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/home/floor/remove`),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      showStatusChange (id, showStatus) {
        this.$confirm(`确定对[id=${id}]进行[状态更新]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          distinguishCancelAndClose: true,
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/home/floor/modifyFloorById`),
            method: 'post',
            data: this.$http.adornData({
              'id': id || undefined,
              'showStatus': showStatus ? 1 : 0
            }, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(action => {
          this.getDataList()
        })
      }
    }
  }
</script>
